package com.cloud.annex.controller;

import com.cloud.annex.service.MinioService;
import com.cloud.common.domain.Result;
import com.cloud.common.domain.vo.SvgParam;
import com.cloud.common.utils.ImageUtils;
import com.cloud.exception.CommonException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "附件管理")
@RestController
@RequestMapping("/annexAction")
public class MinioController {

    @Autowired
    private MinioService minioService;

    @Operation(summary = "文件上传")
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) {
        try {
            if (file != null && !file.isEmpty()) {
                return minioService.upload(file, true);
            } else {
                return Result.message(CommonException.FILE_NO_EXITS.getCode(),
                        CommonException.FILE_NO_EXITS.getMsg());
            }
        } catch (Exception e) {
            return Result.error("操作异常:" + e);
        }
    }

    @Operation(summary = "系统图标上传")
    @PostMapping("/uploadIcon")
    public Result uploadIcon(@RequestParam("file") MultipartFile file) {
        try {
            if (file != null && !file.isEmpty()) {
                return minioService.uploadIcon(file, false);
            } else {
                return Result.message(CommonException.FILE_NO_EXITS.getCode(),
                        CommonException.FILE_NO_EXITS.getMsg());
            }
        } catch (Exception e) {
            return Result.error("操作异常:" + e);
        }
    }

    @Operation(summary = "多文件上传")
    @PostMapping("/moreUpload")
    public Result moreUpload(@RequestParam("file") List<MultipartFile> file) {
        try {
            if (file != null && !file.isEmpty()) {
                return minioService.moreUpload(file, true);
            } else {
                return Result.message(CommonException.FILE_NO_EXITS.getCode(),
                        CommonException.FILE_NO_EXITS.getMsg());
            }
        } catch (Exception e) {
            return Result.error("操作异常:" + e);
        }
    }

    @Operation(summary = "图片压缩上传")
    @PostMapping("/imgZipUpload")
    public Result imgZipUpload(@RequestParam("file") MultipartFile file) {
        try {
            if (file != null && !file.isEmpty()) {
                // 判断上传文件是否是图片
                if (ImageUtils.isImage(file.getOriginalFilename())) {
                    return minioService.uploadZipImage(file, true);
                } else {
                    return Result.message(CommonException.UPLOAD_IMAGE_ERROR.getCode(),
                            CommonException.UPLOAD_IMAGE_ERROR.getMsg());
                }
            } else {
                return Result.message(CommonException.FILE_NO_EXITS.getCode(),
                        CommonException.FILE_NO_EXITS.getMsg());
            }
        } catch (Exception e) {
            return Result.error("操作异常:" + e);
        }
    }

    @Operation(summary = "文件下载")
    @Parameter(name = "fileName", description = "文件名称")
    @PostMapping("/download")
    public Result download(String fileName) {
        if (StringUtils.isEmpty(fileName)) {
            return Result.message(CommonException.FILE_NO_EXITS.getCode(),
                    CommonException.FILE_NO_EXITS.getMsg());
        }
        return minioService.download(fileName);
    }

    @Operation(summary = "多文件下载")
    @Parameters({
            @Parameter(name = "zipName", description = "压缩文件名称"),
            @Parameter(name = "urlList", description = "下载文件名称"),
    })
    @PostMapping("/moreDownload")
    public Result moreDownload(String zipName, List<String> urlList) {
        if (ObjectUtils.isEmpty(urlList)) {
            return Result.message(CommonException.FILE_NO_EXITS.getCode(),
                    CommonException.FILE_NO_EXITS.getMsg());
        }
        return minioService.moreDownload(zipName, urlList);
    }

    @Operation(summary = "存储文件删除")
    @Parameter(name = "fileName", description = "文件名称")
    @GetMapping("/remove")
    public Result remove(String fileName) {
        if (StringUtils.isEmpty(fileName)) {
            return Result.message(CommonException.FILE_NO_EXITS.getCode(),
                    CommonException.FILE_NO_EXITS.getMsg());
        }
        return minioService.remove(fileName);
    }

    @Operation(summary = "创建存储桶")
    @PostMapping("/addBucket")
    public Result addBucket(String bucketName) {
        if (StringUtils.isEmpty(bucketName)) {
            return Result.error("存储桶名称不能为空");
        }
        return minioService.addBucket(bucketName);
    }

    @Operation(summary = "设置桶策略")
    @GetMapping("/inAuth/bucketPolicy")
    public Result bucketPolicy(String bucketName) {
        if (StringUtils.isEmpty(bucketName)) {
            return Result.error("存储桶名称不能为空");
        }
        return minioService.bucketPolicy(bucketName);
    }

    @Operation(summary = "Feign内部调用(查询上传附件路径)")
    @Parameter(name = "fileName", description = "文件名称")
    @RequestMapping(value = "/inAuth/getInAnnexUrl", method = {RequestMethod.POST, RequestMethod.GET})
    public Result getInAnnexUrl(String fileName) {
        if (StringUtils.isEmpty(fileName)) {
            return Result.error("文件名称不能为空");
        }
        return minioService.getInAnnexUrl(fileName);
    }

    @Operation(summary = "根据SVG地址转化PNG地址")
    @PostMapping(value = "/inAuth/svgInvertUrl")
    public Result svgInvertUrl(@RequestBody SvgParam svgParam) {
        if (StringUtils.isEmpty(svgParam.getSvgUrl())) {
            return Result.error("文件地址不能为空");
        }
        return minioService.svgInvertUrl(svgParam);
    }

    @Operation(summary = "根据SVG地址转化PNG地址")
    @Parameter(name = "content", description = "生成二维码内容")
    @PostMapping(value = "/inAuth/getQRCodeUrl")
    public Result getQRCodeUrl(String content) {
        if (StringUtils.isEmpty(content)) {
            return Result.error("文件地址不能为空");
        }
        return minioService.getQRCodeUrl(content);
    }

}
